package router

import (
	"PDF/service"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

func AuthRequired() gin.HandlerFunc {
	return func(c *gin.Context) {
		log.Println("AuthRequired middleware is executing")
		value, err := c.Cookie("S.H.I.T")
		if err != nil || value == "" {
			c.Redirect(http.StatusFound, "/index")
			c.Abort()
			return
		}

		c.Next()
	}
}

func Router() {
	r := gin.Default()

	r.POST("/user/login", service.Login)
	r.POST("/user/register", service.CreateUser)

	private := r.Group("/")
	private.Use(AuthRequired())
	{
		private.POST("/attach/upload", service.Upload)
		private.POST("/attach/filehandle", service.FileHandle)
	}
}
